#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:homework8_3.py
# author:wmf
# datetime:2021/5/18 21:15
# software: PyCharm
'''
this is function description
'''
# import module your need

# 3 编写一个UDP的聊天程序，客户端和服务器端能互相聊天应答；
import threading
import time
from socket import *

# def main():
#     sendInfo_socket = socket(AF_INET, SOCK_DGRAM)
#     dest_addr = ('127.0.0.1', 9999)
#     getInfo_socket = socket(AF_INET, SOCK_DGRAM)
#     local_addr = ('',8888)
#     getInfo_socket.bind(local_addr)
#     user = {}
#     while True:
#         recv_data = getInfo_socket.recvfrom(1024)
#         if user.get(recv_data[1],False) == False:
#             user[recv_data[1]] = recv_data[0].decode('utf-8')
#             print('from%s:%s'%(recv_data[1],recv_data[0].decode('utf-8')))
#         send_data = input("send information:")
#         sendInfo_socket.sendto(send_data.encode('utf-8'), dest_addr)
#         if send_data == 'end':
#             break
#     sendInfo_socket.close()
#     getInfo_socket.close()


s1 = socket(AF_INET, SOCK_DGRAM)
host = '127.0.0.1'
port = 9999

s2 = socket(AF_INET, SOCK_DGRAM)
local_addr = ('', 7777)
s2.bind(local_addr)


def recv():
    while True:
        data = s2.recvfrom(1024)
        print("%s:%s"%(data[1][0],data[0].decode('utf-8')))
        time.sleep(1)


def send():
    while True:
        data = input("")
        s1.sendto(data.encode('utf-8'), (host, port))
        time.sleep(1)


if __name__ == "__main__":
    t1 = threading.Thread(target=recv)
    t2 = threading.Thread(target=send)
    t1.start()
    t2.start()
    t1.join()
    t2.join()
